#include <stdio.h>
#include <stdlib.h>
/* pipe grograming copy file */
FILE *write_file(FILE *dst_fp, void *buf, size_t size, int *done)
{
    if (dst_fp) {
        fwrite(buf, size, 1, dst_fp);
        *done = 1;
    }
    return dst_fp;
}

void *read_file(void *buf, FILE *src_fp, const char *dst_name, size_t file_sz, int *done)
{
    if (buf) {
        if (fread(buf, file_sz, 1, src_fp) > 0) {
            fclose(write_file(fopen(dst_name, "w"),buf, file_sz, done));
        }
    }
    return buf;
}

FILE *copy_file(FILE *src_fp, const char *dst_name, int *done)
{
    if (src_fp) {
        size_t file_sz;
        fseek(src_fp, 0, SEEK_END);
        file_sz = ftell(src_fp);
        fseek(src_fp, 0, SEEK_SET);
        if (file_sz > 0) {
            free(read_file(malloc(file_sz), src_fp, dst_name, file_sz, done));
        }
    }
    return src_fp;
}

int main(int argc, char *argv[])
{
    int done = 0;
    fclose(copy_file(fopen("hello.txt", "r"), "hello3.txt", &done));
    if (done)
        printf("copy ok!\n");
    else
        printf("copy failed!\n");
    return 0;
}
